import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useSalvationStore = defineStore('salvation', () => {
  // 超度申请管理数据
  const transcendenceApplications = ref([
    {
      id: 1,
      name: '李某某',
      applicant: '李家长子',
      applicationTime: '2024-01-15 14:30',
      status: '待审核',
      type: '普通超度',
      reason: '意外身亡，家属申请超度',
      progress: 25
    },
    {
      id: 2,
      name: '王某某',
      applicant: '王家二女',
      applicationTime: '2024-01-15 13:45',
      status: '已批准',
      type: '紧急超度',
      reason: '冤死申请超度',
      progress: 75
    },
    {
      id: 3,
      name: '张某某',
      applicant: '张家三子',
      applicationTime: '2024-01-15 12:20',
      status: '仪式完成',
      type: '特殊超度',
      reason: '业障深重，需特殊仪式',
      progress: 100
    },
    {
      id: 4,
      name: '陈某某',
      applicant: '陈家长女',
      applicationTime: '2024-01-15 11:15',
      status: '效果评估中',
      type: '普通超度',
      reason: '正常死亡，家属申请',
      progress: 90
    },
    {
      id: 5,
      name: '刘某某',
      applicant: '刘家次子',
      applicationTime: '2024-01-15 10:30',
      status: '已完成',
      type: '集体超度',
      reason: '灾难受害者集体超度',
      progress: 100
    }
  ])

  // 功德转移记录数据
  const meritTransfers = ref([
    {
      id: 1,
      fromUser: '李善人',
      toUser: '王某某',
      amount: 5000,
      time: '14:30',
      type: '超度转移',
      status: '已完成',
      description: '为亡魂王某某转移功德，用于超度仪式'
    },
    {
      id: 2,
      fromUser: '张功德主',
      toUser: '陈某某',
      amount: 3000,
      time: '13:45',
      type: '家族转移',
      status: '处理中',
      description: '家族内部功德转移'
    },
    {
      id: 3,
      fromUser: '刘居士',
      toUser: '孙某某',
      amount: 8000,
      time: '12:20',
      type: '布施转移',
      status: '已完成',
      description: '布施所得功德转移给需要者'
    },
    {
      id: 4,
      fromUser: '赵善心',
      toUser: '周某某',
      amount: 2500,
      time: '11:15',
      type: '救赎转移',
      status: '审核中',
      description: '为救赎计划转移功德'
    },
    {
      id: 5,
      fromUser: '钱大德',
      toUser: '吴某某',
      amount: 6000,
      time: '10:30',
      type: '紧急转移',
      status: '已完成',
      description: '紧急情况功德转移'
    }
  ])

  // 救赎计划数据
  const redemptionPlans = ref([
    {
      id: 1,
      title: '贪官张某某救赎计划',
      target: '张某某',
      type: '贪污腐败',
      createTime: '2024-01-15',
      progress: 75,
      status: '进行中',
      tasks: [
        { id: 1, name: '反思贪污行为', status: '已完成', deadline: '2024-01-20' },
        { id: 2, name: '帮助贫困家庭', status: '进行中', deadline: '2024-02-15' },
        { id: 3, name: '公开忏悔', status: '未开始', deadline: '2024-03-01' }
      ],
      supervisor: '判官李明',
      difficulty: '困难'
    },
    {
      id: 2,
      title: '盗贼李某某改造计划',
      target: '李某某',
      type: '偷盗抢劫',
      createTime: '2024-01-14',
      progress: 45,
      status: '进行中',
      tasks: [
        { id: 1, name: '认识盗窃危害', status: '已完成', deadline: '2024-01-18' },
        { id: 2, name: '义务劳动', status: '进行中', deadline: '2024-02-18' },
        { id: 3, name: '返还偷盗物品', status: '未开始', deadline: '2024-03-01' }
      ],
      supervisor: '判官王刚',
      difficulty: '中等'
    },
    {
      id: 3,
      title: '暴徒王某某教化计划',
      target: '王某某',
      type: '暴力犯罪',
      createTime: '2024-01-13',
      progress: 100,
      status: '已完成',
      tasks: [
        { id: 1, name: '愤怒管理训练', status: '已完成', deadline: '2024-01-20' },
        { id: 2, name: '受害者和解', status: '已完成', deadline: '2024-02-10' },
        { id: 3, name: '社区服务', status: '已完成', deadline: '2024-02-28' }
      ],
      supervisor: '判官陈华',
      difficulty: '困难'
    },
    {
      id: 4,
      title: '欺诈陈某某悔过计划',
      target: '陈某某',
      type: '诈骗欺诈',
      createTime: '2024-01-12',
      progress: 20,
      status: '刚开始',
      tasks: [
        { id: 1, name: '诚信教育', status: '进行中', deadline: '2024-01-25' },
        { id: 2, name: '赔偿受害者', status: '未开始', deadline: '2024-02-25' },
        { id: 3, name: '公益宣传', status: '未开始', deadline: '2024-03-15' }
      ],
      supervisor: '判官刘芳',
      difficulty: '中等'
    },
    {
      id: 5,
      title: '杀手刘某某救赎计划',
      target: '刘某某',
      type: '故意杀人',
      createTime: '2024-01-11',
      progress: 0,
      status: '失败',
      tasks: [
        { id: 1, name: '忏悔杀生之罪', status: '失败', deadline: '2024-01-18' },
        { id: 2, name: '超度受害者', status: '未开始', deadline: '2024-02-18' },
        { id: 3, name: '终生忏悔', status: '未开始', deadline: '永久' }
      ],
      supervisor: '判官张伟',
      difficulty: '极难',
      failureReason: '拒绝悔改，态度恶劣'
    }
  ])

  // 功德池统计数据
  const meritPoolStats = ref({
    totalPool: 1586420,
    availablePool: 1234567,
    lockedPool: 351853,
    todayIncrease: 23456,
    usageRate: 77.8,
    lockedRate: 22.2
  })

  // 超度仪式安排数据
  const ritualArrangements = ref([
    {
      id: 1,
      name: '李某某超度仪式',
      date: '2024-01-16',
      time: '上午9:00',
      location: '超度殿一号厅',
      type: '普通超度',
      status: '已安排',
      officiant: '超度师张道长',
      participants: ['李家长子', '李家次女'],
      duration: 120,
      cost: 5000
    },
    {
      id: 2,
      name: '王某某紧急超度仪式',
      date: '2024-01-16',
      time: '下午2:00',
      location: '超度殿二号厅',
      type: '紧急超度',
      status: '进行中',
      officiant: '超度师李道长',
      participants: ['王家二女', '王家三子'],
      duration: 180,
      cost: 8000
    },
    {
      id: 3,
      name: '集体超度仪式',
      date: '2024-01-17',
      time: '上午10:00',
      location: '超度殿大厅',
      type: '集体超度',
      status: '待进行',
      officiant: '超度师王道长',
      participants: ['多个家庭代表'],
      duration: 240,
      cost: 20000
    }
  ])

  // 功德来源数据
  const meritSources = ref([
    {
      id: 1,
      name: '善行积德',
      type: '日常善行',
      dailyIncome: 1200,
      monthlyIncome: 36000,
      description: '日常行善积累的功德',
      status: '活跃'
    },
    {
      id: 2,
      name: '布施捐赠',
      type: '财物布施',
      dailyIncome: 2500,
      monthlyIncome: 75000,
      description: '信众布施捐赠获得的功德',
      status: '活跃'
    },
    {
      id: 3,
      name: '功德主供养',
      type: '专项供养',
      dailyIncome: 5000,
      monthlyIncome: 150000,
      description: '功德主专项供养的功德',
      status: '活跃'
    },
    {
      id: 4,
      name: '法事功德',
      type: '宗教仪式',
      dailyIncome: 3000,
      monthlyIncome: 90000,
      description: '举办法事仪式获得的功德',
      status: '活跃'
    },
    {
      id: 5,
      name: '念经持咒',
      type: '修行功德',
      dailyIncome: 800,
      monthlyIncome: 24000,
      description: '念经持咒修行获得的功德',
      status: '活跃'
    }
  ])

  // 救赎任务模板
  const redemptionTaskTemplates = ref([
    {
      id: 1,
      name: '贪污腐败救赎模板',
      category: '经济犯罪',
      tasks: ['反思贪污行为的危害', '向受害者道歉', '返还非法所得', '参与反腐教育', '义务劳动服务', '帮助贫困家庭'],
      duration: '6个月',
      difficulty: '困难',
      successRate: 65
    },
    {
      id: 2,
      name: '偷盗抢劫救赎模板',
      category: '财产犯罪',
      tasks: ['认识盗窃危害', '向受害者道歉', '返还偷盗物品', '义务劳动', '诚信教育', '职业技能培训'],
      duration: '4个月',
      difficulty: '中等',
      successRate: 78
    },
    {
      id: 3,
      name: '暴力犯罪救赎模板',
      category: '暴力犯罪',
      tasks: ['愤怒管理训练', '心理辅导', '与受害者和解', '社区服务', '暴力后果教育', '冥想修行'],
      duration: '8个月',
      difficulty: '困难',
      successRate: 55
    }
  ])

  // 统计数据
  const salvationStats = ref({
    // 超度申请统计
    transcendenceStats: {
      totalApplications: 156,
      pendingReview: 48,
      approved: 89,
      rejected: 12,
      completed: 134,
      successRate: 87.5,
      todayApplications: 8,
      weeklyApplications: 23
    },
    // 功德转移统计
    meritStats: {
      totalTransfers: 2847,
      todayTransfers: 23,
      pendingTransfers: 8,
      completedTransfers: 2816,
      failedTransfers: 23,
      totalAmount: 15864200,
      todayAmount: 234560,
      averageAmount: 5579
    },
    // 救赎计划统计
    redemptionStats: {
      totalPlans: 189,
      activePlans: 45,
      completedPlans: 127,
      failedPlans: 17,
      successRate: 88.2,
      averageDuration: 165, // 天数
      totalRedeemed: 144
    }
  })

  // Actions
  const addTranscendenceApplication = (application) => {
    const newApp = {
      id: transcendenceApplications.value.length + 1,
      ...application,
      applicationTime: new Date().toLocaleString(),
      status: '待审核',
      progress: 0
    }
    transcendenceApplications.value.unshift(newApp)
  }

  const updateApplicationStatus = (id, status, progress = null) => {
    const app = transcendenceApplications.value.find((item) => item.id === id)
    if (app) {
      app.status = status
      if (progress !== null) {
        app.progress = progress
      }
    }
  }

  const addMeritTransfer = (transfer) => {
    const newTransfer = {
      id: meritTransfers.value.length + 1,
      ...transfer,
      time: new Date().toLocaleTimeString(),
      status: '处理中'
    }
    meritTransfers.value.unshift(newTransfer)
  }

  const updateTransferStatus = (id, status) => {
    const transfer = meritTransfers.value.find((item) => item.id === id)
    if (transfer) {
      transfer.status = status
    }
  }

  const addRedemptionPlan = (plan) => {
    const newPlan = {
      id: redemptionPlans.value.length + 1,
      ...plan,
      createTime: new Date().toISOString().split('T')[0],
      progress: 0,
      status: '刚开始'
    }
    redemptionPlans.value.unshift(newPlan)
  }

  const updatePlanProgress = (id, progress, status = null) => {
    const plan = redemptionPlans.value.find((item) => item.id === id)
    if (plan) {
      plan.progress = progress
      if (status) {
        plan.status = status
      }
    }
  }

  const getApplicationById = (id) => {
    return transcendenceApplications.value.find((item) => item.id === parseInt(id))
  }

  const getTransferById = (id) => {
    return meritTransfers.value.find((item) => item.id === parseInt(id))
  }

  const getPlanById = (id) => {
    return redemptionPlans.value.find((item) => item.id === parseInt(id))
  }

  return {
    // 数据
    transcendenceApplications,
    meritTransfers,
    redemptionPlans,
    meritPoolStats,
    ritualArrangements,
    meritSources,
    redemptionTaskTemplates,
    salvationStats,

    // 方法
    addTranscendenceApplication,
    updateApplicationStatus,
    addMeritTransfer,
    updateTransferStatus,
    addRedemptionPlan,
    updatePlanProgress,
    getApplicationById,
    getTransferById,
    getPlanById
  }
})
